﻿using System;
using System.Data.Common;
using DMS.Base;
using DMS.Business.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DMS.Business.Modules.Organization
{
    /// <summary>
    /// 内部用户
    /// </summary>
    public class User : AuthenticationUser
    {
        #region 数据成员/内部成员
        protected string _EmployeeID;
        protected string _Pinyin;
        protected string _CardCode;
        protected SexTypes _Sex = SexTypes.Unknow;
        protected DateTime _Birthday = DateTime.MinValue;
        protected LigionTypes _Ligion = LigionTypes.Unknow;
        protected string _Nation;
        protected string _Native;
        protected int _Sequence = int.MaxValue;
        protected string _Telephone;
        protected string _disAddress;//常用配送地址, added by Cola

        protected JobTypes _JobType = JobTypes.Other;
        protected string _Labour;
        protected string _UserGrade;
        protected string _PosLevel;
        protected string _PosCurrentLevel;
        protected string _PositionName;
        protected DateTime _JoinInDate;

        internal string _DeptID = "";
        protected Department _Dept = null;

        internal string _BigDeptID = "";
        protected Department _BigDept = null;

        #endregion

        #region 成员属性
        /// <summary>
        /// 上次配送地址（按客户要求，每次配送地址都必须重新填，所以返加空）
        /// </summary>
        public string DisAddress
        {
            get
            {
                return "";
                //return CookieHelper.getCookie(_ID.ToString());
            }
            set
            {
                //string cookieVlaue = CookieHelper.getCookie(_ID.ToString());
                ////如果存在cookie,且和新的值不一样，才更新cookie
                //if (cookieVlaue != null && cookieVlaue != value)
                //{
                //    CookieHelper.setCookie(_ID.ToString(), value, 365 * 50);
                //}
                //if (cookieVlaue == null) //如果不存在cookie,添加cookie
                //{
                //    CookieHelper.setCookie(_ID.ToString(), value, 365 * 50);
                //} 
            }
        }

        public ActorTypes ActorType
        {
            get
            {
                return ActorTypes.User;
            }
        }
        /// <summary>
        /// 员工编号
        /// </summary>
        public string EmployeeID
        {
            get
            {
                return this._EmployeeID;
            }
            set
            {
                this._EmployeeID = value;
            }
        }
        /// <summary>
        /// 姓名拼音
        /// </summary>
        public string Pinyin
        {
            get
            {
                return this._Pinyin;
            }
            set
            {
                this._Pinyin = value;
            }
        }
        /// <summary>
        /// 身份证号
        /// </summary>
        public string CarCode
        {
            get
            {
                return this._CardCode;
            }
            set
            {
                this._CardCode = value;
            }
        }
        /// <summary>
        /// Sex
        /// </summary>
        public SexTypes Sex
        {
            get
            {
                return this._Sex;
            }
            set
            {
                this._Sex = value;
            }
        }
        /// <summary>
        /// 出生年月
        /// </summary>
        public DateTime Birthday
        {
            get
            {
                return this._Birthday.Date;
            }
            set
            {
                this._Birthday = value;
            }
        }
        /// <summary>
        /// 政治面貌
        /// </summary>
        public LigionTypes Ligion
        {
            get
            {
                return this._Ligion;
            }
            set
            {
                this._Ligion = value;
            }
        }
        /// <summary>
        /// 民族
        /// </summary>
        public string Nation
        {
            get
            {
                return this._Nation;
            }
            set
            {
                this._Nation = value;
            }
        }
        /// <summary>
        /// 籍贯
        /// </summary>
        public string Native
        {
            get
            {
                return this._Native;
            }
            set
            {
                this._Native = value;
            }
        }
        /// <summary>
        /// 排序
        /// </summary>
        public int Sequence
        {
            get
            {
                return this._Sequence;
            }
            internal set
            {
                this._Sequence = value;
            }
        }
        /// <summary>
        /// 工作电话
        /// </summary>
        public string Telephone
        {
            get
            {
                return this._Telephone;
            }
            set
            {
                this._Telephone = value;
            }
        }

        /// <summary>
        /// 在职类型
        /// </summary>
        public JobTypes JobType
        {
            get
            {
                return this._JobType;
            }
            set
            {
                this._JobType = value;
            }
        }
        /// <summary>
        /// 劳务公司
        /// </summary>
        public string Labour
        {
            get
            {
                return this._Labour;
            }
            set
            {
                this._Labour = value;
            }
        }
        /// <summary>
        /// 用户职称
        /// </summary>
        public string UserGrade
        {
            get
            {
                return this._UserGrade;
            }
            set
            {
                this._UserGrade = value;
            }
        }
        /// <summary>
        /// 岗位职级
        /// </summary>
        public string PosLevel
        {
            get
            {
                return this._PosLevel;
            }
            set
            {
                this._PosLevel = value;
            }
        }
        /// <summary>
        /// 当前套入职级
        /// </summary>
        public string PosCurrentLevel
        {
            get
            {
                return this._PosCurrentLevel;
            }
            set
            {
                this._PosCurrentLevel = value;
            }
        }
        /// <summary>
        /// 岗位名称
        /// </summary>
        public string PositionName
        {
            get
            {
                return this._PositionName;
            }
            set
            {
                this._PositionName = value;
            }
        }
        /// <summary>
        /// 入职日期
        /// </summary>
        public DateTime JoinInDate
        {
            get
            {
                return this._JoinInDate;
            }
            set
            {
                this._JoinInDate = value;
            }
        }

        /// <summary>
        /// 用户所在部门
        /// </summary>
        public Department Dept
        {
            get
            {
                if (this._DeptID != "" && this._Dept == null)
                {
                    this._Dept = UserHelper.GetDepartmentByID(this._DeptID);
                }
                return this._Dept;
            }
            set
            {
                if (value != null)
                {
                    this._DeptID = value.ID;
                    this._Dept = value;
                }
                else
                {
                    this._DeptID = "";
                    this._Dept = null;
                }
            }
        }

        /// <summary>
        /// 用户所在大部门
        /// </summary>
        public Department BigDept
        {
            get
            {
                if (this._BigDeptID != "" && this._BigDept == null)
                {
                    this._BigDept = UserHelper.GetDepartmentByID(this._BigDeptID);
                }
                return this._BigDept;
            }
            set
            {
                if (value != null)
                {
                    this._BigDeptID = value.ID;
                    this._BigDept = value;
                }
                else
                {
                    this._BigDeptID = "";
                    this._BigDept = null;
                }
            }
        }


        private Department _countUnit;
        /// <summary>
        /// 用户所属的统计部门：
        /// 比如刘宇琛的当前部门是物资管理室，但是他的统计部门是属于行政服务中心
        /// </summary>
        public Department CountUnit
        {
            get
            {
                if (_countUnit == null)
                {
                    _countUnit = this.Dept.CountUnit;
                }
                return _countUnit;
            }
            set
            {
                _countUnit = value;
            }
        }

        private string _countUnitID;
        /// <summary>
        /// 用户所属的统计部门ID：
        /// 比如刘宇琛的当前部门是物资管理室，但是他的统计部门是属于行政服务中心
        /// </summary>
        public string CountUnitID
        {
            get
            {
                if (_countUnitID == null)
                {
                    _countUnitID = this.Dept.CountUnitID;
                }
                return _countUnitID;
            }
            set
            {
                _countUnitID = value;
            }
        }

        ///// <summary>
        ///// 用户配送地址
        ///// </summary>
        //public List<DistributeAdd> DistAdds
        //{
        //    get
        //    {
        //        return MaterialHelper.GetDistAddByUserID(this._ID);
        //    }
        //}        

        #endregion

        #region 构造函数
        public User(string id)
            : base(id)
        {
        }
        #endregion

        #region 成员方法
        public override void Save()
        {
            throw new InvalidOperationException("内部用户信息不能通过本方法更新");
        }

        private const string SQL_UserPassword_Update = @"UPDATE [dbo].[ORG_Users]
                                                                   SET [StaticPWD] = @StaticPWD
                                                                 WHERE [LoginID] = @LoginID";

        public override void ResetPassword(string newPassword)
        {
            this._PasswordCode = this.EncryptPWD(newPassword);
            Database db = DBHelper.Instance;
            DbCommand cmd = db.GetSqlStringCommand(SQL_UserPassword_Update);
            
            DbParameter p_StaticPWD = cmd.CreateParameter();
            p_StaticPWD.ParameterName = "StaticPWD";
            p_StaticPWD.Value = this._PasswordCode;
            cmd.Parameters.Add(p_StaticPWD);

            DbParameter p_LoginID = cmd.CreateParameter();
            p_LoginID.ParameterName = "LoginID";
            p_LoginID.Value = this._LoginID;
            cmd.Parameters.Add(p_LoginID);

            db.ExecuteNonQuery(cmd);
        }
        #endregion

        #region 系统内置角色判断
        /// <summary>
        /// 是否室经理
        /// </summary>
        //public override bool IsManager
        //{
        //    get
        //    {
        //        return this.PositionName.IndexOf("室经理") >= 0;
        //    }
        //}

        /// <summary>
        /// 是否物资管理员室经理
        /// </summary>
        public override bool IsWZSManager
        {
            get
            {
                return IsInRole("物资管理员室经理");
            }
        }

        /// <summary>
        /// 是否服务厅员工
        /// </summary>
        public bool IsBusinessHallUser
        {
            get
            {
                if (this.Dept.Name.IndexOf("服务厅") >= 0 || this.Dept.Name.IndexOf("服营厅") >= 0)
                    return true;
                return false;
            }
        }
        #endregion
    }
}
